<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//加密函数
function lock_url($txt,$key='zhuoyuexiazai'){
	$txt = $txt.$key;
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$nh = rand(0,64);
	$ch = $chars[$nh];
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = base64_encode($txt);
	$tmp = '';
	$i=0;$j=0;$k = 0;
	for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
		$tmp .= $chars[$j];
	}
	return urlencode(base64_encode($ch.$tmp));
}
//解密函数
function unlock_url($txt,$key='zhuoyuexiazai'){
	$txt = base64_decode(urldecode($txt));
	$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	$ch = $txt[0];
	$nh = strpos($chars,$ch);
	$mdKey = md5($key.$ch);
	$mdKey = substr($mdKey,$nh%8, $nh%8+7);
	$txt = substr($txt,1);
	$tmp = '';
	$i=0;$j=0; $k = 0;
	for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
		while ($j<0) $j+=64;
		$tmp .= $chars[$j];
	}
	return trim(base64_decode($tmp),$key);
} 
function p($a){
	echo '<pre>';
	print_r($a);die;
	
}
//二维数组
function  two_dimensional_array($list,$key){
	$ref=[];
	foreach ($list as $rkey=>$val){
		if($val[$key]==0){
			$ref[]=$val;
		}
	}
	
	foreach ($ref as $rkey1=>$val1){
		foreach ($list as $rkey2=>$val2){
			if($val1['id']==$val2[$key]){
				$ref[$rkey1]['children'][]=$val2;
			}
		}
	}
	return $ref;
}
function juli_str($lat,$lng){
	$juli=" ROUND(
			        6378.138 * 2 * ASIN(
			            SQRT(
			                POW(
			                    SIN(
			                        (
			                            $lat * PI() / 180 - lat * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                ) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW(
			                    SIN(
			                        (
			                            $lng * PI() / 180 - lng * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                )
			            )
			        ) * 1000
			    ) AS juli
			";
    return $juli;
}
/**
 * 获取网路图片的长宽
 * @param unknown $url
 * @return string[]|unknown[]
 */
function getImageinfo($url)
{
	$result = array(
			'width'=>'',
			'height'=>'',
			'size'=>'',
	);
	$imageInfo = getimagesize($url);
	$result['width']=$imageInfo[0];
	$result['height']=$imageInfo[1];
	
	$headerInfo = get_headers($url,true);
	$result['size']=$headerInfo['Content-Length'];
	
	return $result;
}

function httpRequest($url,$type = "POST", $keysArr = array(), $headersArr = array(),$timeOut = 30){
    if($type == "POST"){
        $ch = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }

        $aPOST = array();
        foreach ($keysArr as $key => $val) {
            $aPOST[] = $key . "=" . urlencode($val);
        }
        $strPOST = implode("&", $aPOST);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        //带上headers
        if($headersArr){
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArr);
        }


        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);

        $sContent = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
//            var_dump($aStatus);exit;
        curl_close($ch);
        if (intval($aStatus["http_code"]) == 200) {
            if($sContent){
                //echo 111;
                return json_decode($sContent,true) ;
            }
//                echo 2222;
            return $sContent;
        }
    }else{
        $url = combineURL($url, $keysArr);
        $ch = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        if($headersArr){
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArr);
        }

        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);


//            // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
//            curl_setopt($ch, CURLOPT_HEADER, true);
//            // 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
//            curl_setopt($ch, CURLOPT_NOBODY, true);
//            //至关重要，CURLINFO_HEADER_OUT选项可以拿到请求头信息
//            curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        $sContent = curl_exec($ch);

//            if ($error == curl_error($ch)) {
//                var_export($error);
//                return false;
//            }

        $aStatus = curl_getinfo($ch);

//            echo "<pre>";
//            var_export($sContent);
//            var_export($aStatus);
//            exit;

        curl_close($ch);
        if (intval($aStatus["http_code"]) == 200) {
            if($sContent){
                return json_decode($sContent,true) ;
            }
            return $sContent;
        }
    }
}

/**
 * combineURL
 * 拼接url
 * @param string $baseURL 基于的url
 * @param array $keysArr 参数列表数组
 * @return string           返回拼接的url
 */
function combineURL($baseURL, $keysArr)
{
    if (empty($keysArr) || !is_array($keysArr)) return $baseURL;

    $combined = $baseURL . "?";
    $valueArr = array();
    foreach ($keysArr as $key => $val) {
        $valueArr[] = "$key=" . urlencode($val);
    }
    $keyStr = implode("&", $valueArr);
    $combined .= ($keyStr);
    return $combined;
}


function object_to_array($obj) {
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }

    return $obj;
}

function http_request_1($url,$request = 'GET',$params = []){
    $curl = new \Curl\Curl();
    if ($request == 'GET'){
       $res = $curl->get($url,$params);
    }else if($request == 'POST'){
        $res = $curl->post($url,$params);
    }else{
        return false;
    }
    if ($res === false){
        return false;
    }

    $arr = object_to_array($res);
    if (!$arr){
        return false;
    }
    return $arr;
}

/**
 * 递归出父集
 * @param $cate
 * @param $id
 * @return array
 */
function get_top_parentid($cate,$id){
    $arr=array();
    foreach($cate as $v){
        if($v['id']==$id){
            $arr[]=$v;// $arr[$v['id']]=$v['name'];
            $arr=array_merge(get_top_parentid($cate,$v['p_id']),$arr);
        }
    }return $arr;

}

/**
 * 递归出子集
 * @param $data
 * @param $pid
 * @return array
 */
function tree_son($data,$pid){
    $son=[];
    foreach ( $data as $key=>$vo){
        if ($vo['p_id']==$pid){
            $vo['son']=tree_son($data, $vo['id']);
            $son[]=$vo;
        }
    }
    return $son;
}

function tree_children($data,$pid){
    $son=[];
    foreach ( $data as $key=>$vo){
        if ($vo['p_id']==$pid){
            $vo['children']=tree_children($data, $vo['id']);
            $son[]=$vo;
        }
    }
    return $son;
}

//递归获取所有的子分类的ID1
function get_all_child($array,$id){
    $arr = array();
    foreach($array as $v){
        if($v['pid'] == $id){
            $arr[] = $v['id'];
            $arr = array_merge($arr,get_all_child($array,$v['id']));
        };
    };
    return $arr;
}

//递归获取所有的子分类的ID11
function get_child_ids($array,$id){
    $arr = array();
    foreach($array as $v){
        if($v['p_id'] == $id){
            $arr[] = $v['id'];
            $arr = array_merge($arr,get_child_ids($array,$v['id']));
        };
    };
    return $arr;
}

function project_status($status){
    $project_status=[
        1=>'新建',
        2=>'在跟',
        3=>'签约',
        4=>'投产'
    ];
    return $project_status[$status];
}